class Requests {
    static toParamString(obj) {
        // let obj = this;
        return Object.keys(obj).map((key) => {
            return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])
        }).join('&');
    }
    static fetchWithData(url,data) {
        return fetch(url,{
            method: 'post',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            body: Requests.toParamString(data)
        }).then(_=>_.json());
    }
    static getHost() {
        if (location.hostname === 'localhost') {
            return `http://localhost:8080/`;
        } else {
            return `https://www.sunibas.cn:39000/`;
        }
    }
    static setToken(token) {
        return new Promise(s => {
            sessionStorage.setItem('_login_token_',token);
            s();
        });
    }
    static getToken() {
        return new Promise(s => {
            let token = sessionStorage.getItem('_login_token_');
            s(token || '');
        });
    }
    static getBingImage() {
        return fetch('./../jsons/bingPic.json').then(_ => _.json());
    }
    static login(user,password) {
        return fetch(`${Requests.getHost()}user/login?username=${user}&password=${password}`,{ method: 'post' })
            .then(_ => _.json()).then(d => {
                if (d.code === 200) {
                    return d.data.id;
                } else {
                    throw new Error(d.message);
                }
            });
    }
    static getAdminQuestion(token) {
        return fetch(`${Requests.getHost()}user/getAdminQuestion?token=${token}`,{ method: 'post' })
            .then(_ => _.json()).then(d => {
                if (d.code === 0) {
                    return d.data;
                } else {
                    throw new Error(d.message);
                }
            });
    }
    static checkAdminQuestion(id,answer,token) {
        return fetch(`${Requests.getHost()}user/checkAdminQuestion?token=${token}&id=${id}&answer=${answer}`,{ method: 'post' })
            .then(_ => _.json()).then(d => {
                if (d.code === 0) {
                    return d.data;
                } else {
                    throw new Error(d.message);
                }
            })
    }
    static checkLogin(token) {
        return fetch(`${Requests.getHost()}user/checkLogin?token=${token}`,{ method: 'post' })
            .then(_ => _.json()).then(d => {
                if (d.code === 200) {
                    return {
                        type: d.message === '已登录',
                        id: d.data // userid
                    };
                } else {
                    throw new Error(d.message);
                }
            })
    }
    static checkAdmin(token) {
        return fetch(`${Requests.getHost()}user/checkAdmin?token=${token}`,{ method: 'post' })
            .then(_ => _.json()).then(d => {
                if (d.code === 200) {
                    return '';
                } else {
                    return d.message;
                }
            })
    }

    // pages-controller
    static getPages() {
        return fetch(Requests.getHost() + 'pages/listAll',{method:'post'}).then(_=>_.json());
    }
    static updatePage(pageObj,token) {
        return Requests.fetchWithData(`${Requests.getHost()}pages/update`,{
            ...pageObj,
            token
        });
    }
    static addPage(pageObj,token) {
        return Requests.fetchWithData(`${Requests.getHost()}pages/add`,{
            ...pageObj,
            token
        });
    }
    static deletePage(id,token) {
        return fetch(`${Requests.getHost()}pages/delete?id=${id}&token=${token}`,{method:'post'});
    }
}
